package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.dic.SexEnum;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 处方的药品信息Controller
 *
 * @author ruoyi
 * @date 2023-05-13
 */
@Controller
@RequestMapping("/system/refund")
public class TPrescriptionRefundController extends BaseController {
    private String prefix = "system/refund";

    @Autowired
    private ITPrescriptionHistoryInfoService tPrescriptionHistoryInfoService;

    @Autowired
    private ITPrescriptionRecordsService tPrescriptionRecordsService;

    @Autowired
    private ITDrugInfoService tDrugInfoService;

    @Autowired
    private ITPrescriptionDrugInfoService tPrescriptionDrugInfoService;

    @Autowired
    private ITPrescriptionNurseHisService tPrescriptionNurseHisService;


    @RequiresPermissions("system:refund:view")
    @GetMapping()
    public String info() {
        return prefix + "/refund";
    }

    /**
     * 查询处方的药品信息列表
     */
    @RequiresPermissions("system:refund:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPrescriptionHistoryInfo tPrescriptionHistoryInfo) {
        startPage();
        // 处方信息
        List<TPrescriptionHistoryInfo> list = tPrescriptionHistoryInfoService.selectOtherHistoryInfoList1(tPrescriptionHistoryInfo);
        setDaysDetail(list);

        list.sort((person1, person2) -> (int) (person1.getAdd_time().getTime() - person2.getAdd_time().getTime()));
        return getDataTable(list);
    }

    private void setDaysDetail(List<TPrescriptionHistoryInfo> datas) {
        datas.stream().peek(e -> {
            int less = e.getDaysDetail() - e.getOver_days();
            e.setDaysDetailDesc(e.getDaysDetail() + "天余" + less);
            e.setCustomerSex(SexEnum.getByCode(e.getCustomerSex()));
        }).collect(Collectors.toList());
    }



    /**
     * 展示处方信息
     */
    @RequiresPermissions("system:refund:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        TPrescriptionRecords tPrescriptionRecords = tPrescriptionRecordsService.selectTPrescriptionRecordsById(id);
        mmap.put("tPrescriptionRecords", tPrescriptionRecords);
        mmap.put("pharmacyid", getSysUser().getUserId());
        return prefix + "/info";
    }



    /**
     * 修改-退款
     */
    @RequiresPermissions("system:refund:edit")
    @Log(title = "处方的药品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/tuikuan")
    @ResponseBody
    public AjaxResult tuikuan(@RequestBody List<TPrescriptionDrugInfo> tPrescriptionDrugInfos) {
        int i = 0;
        if (tPrescriptionDrugInfos.size() > 0) {
            // 退款
            for (TPrescriptionDrugInfo tPrescriptionDrugInfo : tPrescriptionDrugInfos) {
                tPrescriptionDrugInfo.setRefundTime(DateUtils.getNowDate());
                // 更新处方中药品的退款数量与退款状态
                i += tPrescriptionDrugInfoService.updateDrugInfo(tPrescriptionDrugInfo);
                // 增加药品库存数量
                tPrescriptionDrugInfoService.updataDrugNumber(tPrescriptionDrugInfo);
            }

            // 更新药房操作人id
            SysUser user = getSysUser();
            TPrescriptionRecords tPrescriptionRecords = new TPrescriptionRecords();
            tPrescriptionRecords.setPharmacyid(user.getUserId());
            tPrescriptionRecords.setId(tPrescriptionDrugInfos.get(0).getPrescriptionid());
            tPrescriptionRecordsService.updateTPrescriptionRecords1(tPrescriptionRecords);
        }
        return toAjax(i);
    }


}
